package TimeToWork.Android;

import android.os.Parcel;
import android.os.Parcelable;



public class DCWeek implements Parcelable {
    
    @SuppressWarnings("unused")
    private static final String TAG = "DCWeek";
    private static final int DAYS_OF_WEEK = 7;

    private MyTime week;
    
    private DCDay[] DCDays = new DCDay[DAYS_OF_WEEK];
    
    public DCWeek(MyTime week) {
        this.week = week.copy().getStartTime(MyTime.StartTime.WEEK_OF_YEAR);
        
        
        for (int i = 0; i < DAYS_OF_WEEK ;i++){
            this.DCDays[i] = new DCDay(this.week.getOffset(MyTime.DAY_OF_YEAR, i));
        }
    }
    
    public DCWeek(Parcel in) {
        this.week = in.readParcelable(MyTime.class.getClassLoader());
        this.DCDays = (DCDay[])in.readParcelableArray(DCDay.class.getClassLoader());
    }
    
    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(this.week, flags);
        out.writeParcelableArray(this.DCDays, flags);
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<DCWeek> CREATOR = new Parcelable.Creator<DCWeek>() {
        public DCWeek createFromParcel(Parcel in) {
            return new DCWeek(in);
        }

        public DCWeek[] newArray(int size) {
            return new DCWeek[size];
        }
    };
    
    public DCDay getDCDay(int i) {
        return this.DCDays[i-1];
    }
    
    public MyTime getWeek() {
        return this.week;
    }
    
    public String getStrWeek() {
        return this.week.toString(MyTime.TimeFormat.W);
    }
    
    public String getStrYear() {
        return this.week.toString(MyTime.TimeFormat.Y);
    }

    public TimePeriod getWeekTimePeriod() {
        return new TimePeriod(this.week.getStartTime(MyTime.StartTime.WEEK_OF_YEAR), this.week.getEndTime(MyTime.StartTime.WEEK_OF_YEAR));
    }
    
    public void addWorkTimePeriod(WorkTimePeriod workTimePeriod) {
        this.DCDays[workTimePeriod.getStartTime().get(MyTime.DAY_OF_WEEK)-1].addWorkTimePeriod(workTimePeriod);
    }
    
    public void addEvent(Event event) {
        this.DCDays[event.getDate().get(MyTime.DAY_OF_WEEK)-1].addEvent(event);
    }
    
    public TimePeriodLength getTotWorkTime() {
        TimePeriodLength totWorkTime = new TimePeriodLength();
        for (int i = 0; i < DAYS_OF_WEEK; i++){
            totWorkTime.addTime(DCDays[i].getTotWorkTime());
        }
        return totWorkTime;
    }
    
    public String getStrTotWorkTime() {
        return this.getTotWorkTime().toString(TimePeriodLength.TimeFormat.HM);
    }
    
    public String getStrDateDay(int i, MyTime.TimeFormat format) {
        return this.DCDays[i-1].getStrDate(format);
    }
    
    public String getStrDayTotWorkTime(int i, TimePeriodLength.TimeFormat format) {
        return this.DCDays[i-1].getStrDayTotWorkTime(format);
    }
    
    public MyTime getDateDay(int i) {
        return this.DCDays[i-1].getDateDay();    
    }
    
    public TimePeriodLength getTotAbsence() {
        TimePeriodLength totAbsence = new TimePeriodLength();
        for (int i = 0; i < DAYS_OF_WEEK; i++){
            totAbsence.addTime(DCDays[i].getTotAbsence());
        }
        return totAbsence;
    }
    
    public String getStrTotAbsence() {
        return this.getTotAbsence().toString(TimePeriodLength.TimeFormat.HM);
    }
    
    public TimePeriodLength getTotFlex() {
        TimePeriodLength calcTotFlex = this.getTotWorkTime();
        calcTotFlex.addTime(this.getTotAbsence());
        calcTotFlex.subTime(new TimePeriodLength(40*60*60*1000));
        return calcTotFlex;
    }
    
    public String getStrTotFlex() {
        return this.getTotFlex().toString(TimePeriodLength.TimeFormat.HM);
    }
    
    public long getId(int i) {
        return this.DCDays[i-1].getDateDay().getTimeInMillis();
    }
}